package com.k12.common.model.resp;

import com.k12.common.model.constant.ResultCode;
import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serial;
import java.io.Serializable;

/**
 * 通用响应封装类
 * @param <T> 响应数据类型
 */
@Data
@Accessors(chain = true)
public class R<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String msg;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 时间戳（毫秒）
     */
    private Long timestamp = System.currentTimeMillis();

    // 私有构造器
    private R() {}

    // 私有构造器（带状态码和消息）
    private R(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    // 私有构造器（带状态码、消息和数据）
    private R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    // ---------------------- 成功响应 ----------------------

    public static <T> R<T> success() {
        return new R<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg());
    }

    public static <T> R<T> success(T data) {
        return new R<>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMsg(), data);
    }

    public static <T> R<T> success(String msg, T data) {
        return new R<>(ResultCode.SUCCESS.getCode(), msg, data);
    }

    // ---------------------- 失败响应 ----------------------

    public static <T> R<T> failed() {
        return new R<>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMsg());
    }

    public static <T> R<T> failed(String msg) {
        return new R<>(ResultCode.FAILED.getCode(), msg);
    }

    public static <T> R<T> failed(Integer code, String msg) {
        return new R<>(code, msg);
    }

    public static <T> R<T> failed(ResultCode resultCode) {
        return new R<>(resultCode.getCode(), resultCode.getMsg());
    }

    public static <T> R<T> failed(ResultCode resultCode, String customMsg) {
        return new R<>(resultCode.getCode(), customMsg);
    }

    // ---------------------- 业务异常响应 ----------------------

    public static <T> R<T> businessFail(String msg) {
        return new R<>(ResultCode.BUSINESS_ERROR.getCode(), msg);
    }

    public static <T> R<T> businessFail(String msg, T data) {
        return new R<>(ResultCode.BUSINESS_ERROR.getCode(), msg, data);
    }

    // ---------------------- 参数校验响应 ----------------------

    public static <T> R<T> validateFailed() {
        return new R<>(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg());
    }

    public static <T> R<T> validateFailed(String msg) {
        return new R<>(ResultCode.VALIDATE_FAILED.getCode(), msg);
    }

    public static <T> R<T> validateFailed(T data) {
        return new R<>(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getMsg(), data);
    }

    // ---------------------- 其他通用响应 ----------------------

    public static <T> R<T> unauthorized(String msg) {
        return new R<>(ResultCode.UNAUTHORIZED.getCode(), msg);
    }

    public static <T> R<T> forbidden(String msg) {
        return new R<>(ResultCode.FORBIDDEN.getCode(), msg);
    }

    public static <T> R<T> notFound(String msg) {
        return new R<>(ResultCode.NOT_FOUND.getCode(), msg);
    }
}